package com.pansoft.openplanet.widget;

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import androidx.appcompat.app.AlertDialog;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.pansoft.openplanet.R;
import com.utilcode.util.ToastUtils;

/**
 * 导出私钥的dialog
 *
 * @author yqs
 */

public class ExportPrivateKeyDialog {

    private Activity mContext;
    private int mWidth;
    private int mHeight;
    private OnButtonClickListener onButtonClickListener;


    /**
     * 面板占屏幕宽度的百分比
     */
    private static final float WIDTHFRACTION = 0.8f;
    private AlertDialog alertDialog;


    private View rootView;
    private EditText editText;

    private TextView title;
    private TextView tvTips;
    private TextView tvMessage;
    private Button butCopy;


    /**
     * 用户是否可以中途取消下载
     */
    @Deprecated
    public void setCanCancle(boolean canCancle) {
        this.canCancle = canCancle;

    }

    /**
     * 是否可以取消显示
     */
    private boolean canCancle = true;


    private void getWindowWidthAndHeight() {
        WindowManager m = mContext.getWindowManager();
        Display d = m.getDefaultDisplay();
        mWidth = d.getWidth();
        mHeight = d.getHeight();
    }


    public ExportPrivateKeyDialog(Activity context) {
        this.mContext = context;
        getWindowWidthAndHeight();
        init();
    }


    /**
     * 设置标题
     *
     * @param msg
     */
    public void setTitleTextView(String msg) {
        title.setText(msg);
    }

    /**
     * 设置tio
     *
     * @param msg
     */
    public void setTips(String msg) {
        tvTips.setText(msg);
    }

    /**
     * 设置按钮提示
     *
     * @param msg
     */
    public void setButonText(String msg) {
        butCopy.setText(msg);
    }

    /**
     * 设置私钥内容
     *
     * @param msg
     */
    public void setMessage(String msg) {
        tvMessage.setText(msg);
    }

    private void init() {

        rootView = LayoutInflater.from(mContext).inflate(R.layout.talk_chain_dialog_export_privatekey_dialog, null);
        title = rootView.findViewById(R.id.title);
        tvTips = rootView.findViewById(R.id.tv_tips);
        tvMessage = rootView.findViewById(R.id.tv_message);
        butCopy = rootView.findViewById(R.id.but_copy);
        ImageView ivClose = rootView.findViewById(R.id.iv_close);
        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        butCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onButtonClickListener != null) {
                    onButtonClickListener.onclick(0);
                    return;
                }

                ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                // 将文本内容放到系统剪贴板里。
                cm.setText(tvMessage.getText().toString());
                ToastUtils.showShort(R.string.chat_copy_to_clipboard);
                dismiss();
            }
        });

        alertDialog = new AlertDialog.Builder(mContext)
                .setCancelable(false).create();
        alertDialog.setView(rootView);
    }


    public void show() {
        alertDialog.show();
        Window window = alertDialog.getWindow();
        //不这么写不行，圆角无效，圆角无效
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setDialogSize(alertDialog, rootView);


    }


    public void dismiss() {
        alertDialog.dismiss();
    }

    /**
     * 根据屏幕的宽度来设置面板的宽度
     * 高度自适应
     *
     * @param alertDialog
     * @param view
     */
    private void setDialogSize(AlertDialog alertDialog, View view) {
        //为获取屏幕宽、高
        WindowManager.LayoutParams p = alertDialog.getWindow().getAttributes();  //获取对话框当前的参数值
        p.height = WindowManager.LayoutParams.WRAP_CONTENT;
        p.width = (int) (mWidth * WIDTHFRACTION);
        p.gravity = Gravity.CENTER;
        alertDialog.getWindow().setAttributes(p);
    }

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }

    public interface OnButtonClickListener {
        void onclick(int position);
    }
}
